
// pages/upinfo/upinfo.js
var util = require('../../utils/util')
var DATE = util.formatDate(new Date())
var api = require('../../config/api.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        userDto: {
            nickName: '',
            phone: '',
            address: '',
            province: '',
            city: '',
            district: '',
            longitude: 0,
            latitude: 0,
            suggestCode:'',
            title:'',
            detail:'',
            userId:''


        }

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        //获取当前数据库用户
        this.inituser();
    },


    //初始化用户
    inituser() {
        var userinfo = wx.getStorageSync('userInfoDto');
        console.log(userinfo)
        if (typeof (userinfo.nickname) != "undefined") {
            this.setData({
                ['userDto.nickName']: userinfo.nickname,
            })
        }
        if (typeof (userinfo.phone) != "undefined") {
            this.setData({
                ['userDto.phone']: userinfo.phone,
            })
        }
        if (typeof (userinfo.province) != "undefined") {
            this.setData({
                ['userDto.province']: userinfo.province,
            })
        }
        if (typeof (userinfo.city) != "undefined") {
            this.setData({
                ['userDto.city']: userinfo.city,
            })
        }
        if (typeof (userinfo.district) != "undefined") {
            this.setData({
                ['userDto.district']: userinfo.district,
            })
        }
        if (typeof (userinfo.address) != "undefined") {
            this.setData({
                ['userDto.address']: userinfo.address,
            })
        }
        if (typeof (userinfo.userId) != "undefined") {
            this.setData({
                ['userDto.userId']: userinfo.userId,
            })
        }

    },

//用户名称
bindinputtitle(event) {
    this.data.userDto.title = event.detail.value
},
//用户名称
bindinputdetail(event) {
    this.data.userDto.detail = event.detail.value
},

 //保存提交
 save() {
    if (this.data.userDto.title == '' || this.data.userDto.title == null) {
        util.showErrorToast('标题不能为空！');
        return false
    }
    if (this.data.userDto.detail == '' || this.data.userDto.detail == null) {
        util.showErrorToast('反馈意见不能为空！');
        return false
    }
    //提交上报数据
    let dataup = this.data.userDto;
    let that = this;
    console.log(JSON.stringify(dataup))
    that.loadModal();
    util.request(api.pullInfo, JSON.stringify(dataup), 'POST').then(function (res) {
        console.log(res)
        if (res.code == 200) {
            that.closeModal();
            //刷新界面
            console.log("========")
            wx.showModal({
                title: '反馈成功!',
                content: '感谢您对我们在管道保护工作中所做出的贡献和支持。',
                confirmText: '确定',
                success: res => {
                     //刷新界面
                    wx.navigateBack({})
                }
            })

        } else {
            util.showErrorToast(res.message);
            that.closeModal();
        }

    }).catch((err) => {
        that.closeModal();
        console.log("登录远程服务器catch:" + err);
        //reject(err);
    });


},

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },


     //进度条
     loadModal() {
        this.setData({
            loadModal: true
        })
    },
    closeModal() {
        this.setData({
            loadModal: false
        })
    },

})